package com.zhangj.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zhangj.springcloud.handler.MyBlockHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName FlowimitController
 * @Description
 * @Author zhangjie
 * @Date 2022/1/19 14:16
 * @Version 1.0
 */

@RestController
@Slf4j
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() {
        try {
            Thread.sleep(300);
            System.out.println(1/0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }


    /**
     * value = "testHotKey"  方法的别名,控制中心就是通过这个来匹配的
     * blockHandler = "deal_testHotKey"  降级处理的属性-->只有违法了降级策略才会走,抛异常不走 值为降级的方法名
     *  "@SentinelResource" 也有fallback 和 @HystrixCommand的 fallback 一样的
     * "@HystrixCommand的" fallback属性也是方法名,但是与blockHandler不同
     *
     * 规则是按照uri路径进行控制(/testHotKey),不会执行自己的降级方法,
     * 要想走自己的需要根据自己配置的value = "testHotKey"配置降级策略
     *value = "testHotKey" 按照这个去匹配的降级策略,可以走自定义的降级策略
     * @param p1
     * @param p2
     * @return
     */
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandlerClass = MyBlockHandler.class,blockHandler = "deal_testHotKey",fallback = "fallback_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2) {
        int age = 10/0;
        return "------testHotKey";
    }


    public String fallback_testHotKey (String p1, String p2,Throwable throwable ){
        return "------方法有异常fallback,o(╥﹏╥)o";
    }
}
